import { UIMgr } from "../core/UIMgr";
import { Utils } from "../core/Utils";
import { DataMgr } from "../core/DataMgr";
import { Config } from "../core/Config";
import OnFire from "../core/OnFire";
import { Common } from "./Common";
import { JsonMgr } from "../core/JsonMgr";
import { ADMgr } from "../core/AdMgr";
import { EventKey } from "./Key";
import { Partner } from "../core/Partner";
import { AudioMgr } from "../core/AudioMgr";

cc.Class({
    extends: cc.Component,

    properties: {
        layerScene: cc.Node,
        layerPanel: cc.Node,
        layerToast: cc.Node,
        layerBlockInput: cc.Node,
        pfbToast: cc.Prefab,
    },

    onLoad() {
        window.G = window.G || {};
        G.game = this;

        this._requireCores();

        this._initToastPool();

        this._initCanvasFit();

        this._registEvents();

        this._loadSceneBoot();
    },

    // 屏幕适配
    _initCanvasFit() {
        const canvas = this.node.getComponent(cc.Canvas);
        let designScale = canvas.designResolution.width / canvas.designResolution.height;
        if (designScale <= cc.view.getCanvasSize().width / cc.view.getCanvasSize().height) {
            canvas.fitHeight = true;
            canvas.fitWidth = false;
        } else {
            canvas.fitHeight = false;
            canvas.fitWidth = true;
        }
    },

    _requireCores() {
        G.ui = UIMgr;
        G.ui.setRoot(this.layerPanel);

        G.utils = Utils;
        G.data = DataMgr;
        G.config = Config;
        G.onFire = new OnFire();
        G.common = Common;
        G.json = JsonMgr;
        G.ad = ADMgr;
        G.partner = Partner;
        G.partner.init(null,null);
        G.audio = AudioMgr;
        G.audio.init();
    },

    _registEvents() {
        G.onFire.on(EventKey.EVENT_BLOCK_INPUT, this._blockInput, this);
        G.onFire.on(EventKey.EVENT_CANCEL_BLOCK_INPUT, this._cancelBlockInput, this);
    },

    _loadSceneBoot() {
        this.loadScene('SceneBoot');
    },

    ////////////////////////////////toast//////////////////////////
    _initToastPool() {
        G.utils.createPool('nodePoolToast', this.pfbToast, 1);
    },

    toast(content, delay) {
        let toast = G.utils.getPoolNode('nodePoolToast');
        if (toast) {
            toast.parent = this.layerToast;
            toast.getComponent('ItemToast').init(content, delay);
        }
    },
    ////////////////////////////////toast//////////////////////////

    _blockInput() {
        this.layerBlockInput.active = true;
    },
    _cancelBlockInput() {
        this.layerBlockInput.active = false;
    },

    ////////////////////////////////场景////////////////////////////
    getCurrScene() {
        return this.mCurrScene;
    },

    loadScene(targetScene, cb) {
        // 传入空的名字，不做任何操作
        if (!targetScene) {
            return;
        }
        if (this.isSceneLoading) {
            return;
        }
        // 已经在当前场景，不做任何操作（）
        if (targetScene === this.mCurrScene) {
            return;
        }

        this.isSceneLoading = true;
        this._blockInput();
        let url = 'scenes/' + targetScene;
        cc.loader.loadRes(url, cc.Prefab, (err, prefab) => {
            if (err) {
                console.log('加载场景:失败' + err);
                this.cancelBlockInput();
                return;
            }

            if (this.mCurrScene) {
                let oldScene = this.layerScene.getChildByName(this.mCurrScene);
                if (oldScene) {
                    oldScene.destroy();
                }
            }

            this.mCurrScene = targetScene;

            let node = cc.instantiate(prefab);
            node.parent = this.layerScene;

            this.isSceneLoading = false;
            cb && cb();
            G.ui.closeAllUI();
            this._cancelBlockInput();
        })
    },

    preloadScene(sceneName, progressCallback, cb) {
        let url = 'scenes/' + sceneName;
        cc.loader.loadRes(url, cc.Prefab, progressCallback, (err, prefab) => {
            if (err) {
                console.log(err)
                this.toast('预加载场景' + sceneName + '失败');
                return;
            }
            cb && cb();
        });
    },

    // update (dt) {},
});
